#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import time
import subprocess

from mie.cmdsplit import cmdSplit


def help():
    print("sleeprun: sleep before/after run command.")
    print("Usage: sleeprun --b --a cmd args ...")


def main():
    cmd = cmdSplit(sys.argv)

    b = cmd.opts_nth("b", 0)
    a = cmd.opts_nth("a", 0)

    if b is not None:
        time.sleep(float(b))

    if cmd.args:
        subprocess.call(cmd.args, shell=False)
    else:
        help()
        return

    if a is not None:
        time.sleep(float(a))


if __name__ == "__main__":
    try:
        main()
    except:
        help()
